﻿using Sys.Common;
using Sys.Core.Sys;
using Sys.Model;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Sys.Controls
{
    public class Menus : WebControl
    {
        protected override void Render(HtmlTextWriter wt)
        {
            wt.Write(CreateHtml());
        }

        public string CreateHtml()
        {
            SysUser user = UserBll.CurrentUser;

            if (!user.IsSuper)
            {
                var roles = StringHelper.JoinField(RoleBll.I.EntitiesByUserID(user.ID).Where(m => m.IsVisible).ToList(), "ID");

                var rmbs = RoleMenuButtonBll.I.Entities.Where(m => roles.Contains(m.RoleID) && m.SysMenuButton.SysButton.Type.Equals("Purview") && m.SysMenuButton.SysMenu.IsVisible).OrderBy(m => m.SysMenuButton.SysMenu.IntSort).ToList();
                var umbs = UserMenuButtonBll.I.Entities.Where(m => m.UserID.Equals(user.ID) && m.SysMenuButton.SysButton.Type.Equals("Purview") && m.SysMenuButton.SysMenu.IsVisible).OrderBy(m => m.SysMenuButton.SysMenu.IntSort).ToList();

                var list = new List<SysMenu>();

                #region 筛选重复

                foreach (var item in umbs)
                {
                    list.Add(item.SysMenuButton.SysMenu);
                }

                foreach (var item in rmbs)
                {
                    bool bit = true;

                    foreach (var temp in list)
                    {
                        if (item.SysMenuButton.MenuID.Equals(temp.ID))
                        {
                            bit = false; break;
                        }
                    }

                    if (bit) { list.Add(item.SysMenuButton.SysMenu); }
                }

                #endregion

                return ManageMenus(list.OrderBy(m => m.IntSort).ToList());
            }
            else
            {
                return ManageMenus(MenuBll.I.SortEntities.Where(m => m.IsVisible).ToList());
            }
        }

        #region 菜单生成

        /// <summary>
        /// 生成菜单
        /// </summary>
        /// <param name="list">菜单列表</param>
        /// <returns>菜单HTML内容</returns>
        private string ManageMenus(List<SysMenu> list)
        {
            StringBuilder sb = new StringBuilder();

            foreach (var model in list)
            {
                if (string.IsNullOrEmpty(model.Parent) && !model.Type.Equals("Embed"))
                {
                    sb.Append("<div title='" + model.Name + "' data-options='iconCls:\"" + model.Icon + "\"'>");
                    sb.Append("<ul class='easyui-tree' data-options='lines:true,loadFilter:MenuloadFilter,onClick:MenuOnClick'>");
                    sb.Append(GetChildren(model.ID, list, true));
                    sb.Append("</ul>");
                    sb.Append("</div>");
                }
            }

            return sb.ToString();
        }

        /// <summary>
        /// 菜单递规
        /// </summary>
        /// <param name="parentid">父级ID</param>
        /// <param name="list">菜单列表</param>
        /// <returns>菜单HTML内容</returns>
        private string GetChildren(string parentid, List<SysMenu> list, bool first)
        {
            StringBuilder sb = new StringBuilder();

            foreach (var item in list)
            {
                if (item.Parent.Equals(parentid) && !item.Type.Equals("Embed"))
                {
                    string href = item.Link + "?id=" + item.ID;
                    sb.Append("<li iconCls='" + item.Icon + "' data-options='attributes:{href:\"" + href + "\",type:\"" + item.Type + "\"}'><span>" + item.Name + "</span>");
                    sb.Append(GetChildren(item.ID, list, false));
                    sb.Append("</li>");
                }
            }

            if (!string.IsNullOrEmpty(sb.ToString()) && !first)
            {
                return "<ul>" + sb.ToString() + "</ul>";
            }

            return sb.ToString();
        }

        #endregion
    }
}
